package com.code.translate.utils;

import java.io.*;
import java.nio.charset.Charset;

/**
 * @author ggh
 * @date 2022/7/13
 */
public class IOUtils {

    private IOUtils() {
    }

    /**
     * 读取流中信息
     *
     * @param is io流
     * @return
     * @throws IOException
     */
    public static String readInputStream(InputStream is) {
        if (is == null) {
            return "";
        }
        BufferedInputStream bis = new BufferedInputStream(is);
        StringBuffer stringBuffer = new StringBuffer();
        try {
            byte[] bus = new byte[1024];
            int n = -1;
            while ((n = bis.read(bus)) != -1) {
                stringBuffer.append(new String(bus, 0, n, Charset.forName("utf-8")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 读取流中信息(包含中文)
     *
     * @param is io流
     * @return
     * @throws IOException
     */
    public static String readInputStreamWithResponse(InputStream is, String charset) throws UnsupportedEncodingException {
        if (is == null) {
            return "";
        }
        if (charset == null) {
            charset = Charset.defaultCharset().displayName();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
        StringBuffer stringBuffer = new StringBuffer();
        try {
            char[] chars = new char[1024];
            int n = -1;
            while ((n = reader.read(chars)) != -1) {
                new String(chars);
                stringBuffer.append(new String(chars, 0, n));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 读取流中信息到字节数组中
     *
     * @param is
     * @return
     */
    public static byte[] readInputStreamToByteArray(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        try {
            byte[] bus = new byte[2048];
            int n = -1;
            while ((n = bis.read(bus)) != -1) {
                baos.write(bus, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return baos.toByteArray();
    }

    /**
     * 将字节数组写入到流中
     *
     * @return
     */
    public static void writeInputStreamFromByteArray(OutputStream os, byte[] bus) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(os);
        bos.write(bus, 0, bus.length);
        bos.flush();
    }

    /**
     * 读取流中信息并根据指定字符集转换
     *
     * @param is io流
     * @return
     * @throws IOException
     */
    public static String readInputStream(InputStream is, Charset charset) {
        if (is == null) {
            return "";
        }
        BufferedInputStream bis = new BufferedInputStream(is);
        StringBuffer stringBuffer = new StringBuffer();
        try {
            byte[] bus = new byte[1024];
            int n = -1;
            while ((n = bis.read(bus)) != -1) {
                stringBuffer.append(new String(bus, 0, n, charset));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 向指定输出流中写入数据
     *
     * @param is io流
     * @return
     * @throws IOException
     */
    public static void writeStream(InputStream is, OutputStream os, Charset charset) {
        if (is == null || os == null) {
            return;
        }
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(os);

        try {
            byte[] bus = new byte[2048];
            int n = -1;
            while ((n = bis.read(bus)) != -1) {
                bos.write(bus, 0, n);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
